package a51qindian.com.comprehensive;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;

import java.util.ArrayList;
import java.util.List;

import io.rong.imkit.RongIM;

/**
 * Author: Li
 * Date: 2017/9/11 0011
 * Description: App
 */


public class App extends MultiDexApplication {

    @Override
    protected void attachBaseContext(Context base) {
        MultiDex.install(this);
        super.attachBaseContext(base);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (getApplicationInfo().packageName
                .equals(getCurProcessName(getApplicationContext())) ||
                "io.rong.push".equals(getCurProcessName(getApplicationContext()))) {
            RongIM.init(this);
        }
    }

    public static List<Activity> activityList;
    public static void addActivity(Activity activity){
        if (activityList == null) {
            activityList=new ArrayList<>();
        }
        if (activityList.contains(activity)) {
            return;
        }
        activityList.add(activity);
    }
    public static void removeActivity(Activity activity){
        if (activityList == null) {
            return;
        }
        if (activityList.contains(activity)) {
            activityList.remove(activity);
        }
    }
    public static void finishAll(){
        if (activityList==null) {
            return;
        }
        for (Activity activity : activityList) {
            activity.finish();
        }
    }
    public static void clearActivity(){
        activityList.clear();
    }
    /**
     * 获取当前进程名
     * @param context   上下文
     * @return  进程名
     */
    public static String getCurProcessName(Context context) {
        int pid = android.os.Process.myPid();

        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
            if (appProcess.pid == pid)
                return appProcess.processName;
        }
        return null;
    }

}
